<!--
 * Copyright © 2023
 * @Author: zhangxiang
 * @Date: 2023-03-30 16:20:52
 * @LastEditors: zhangxiang
 * @LastEditTime: 2023-03-31 17:55:34
 * @Description:
 *
-->

<template>
  <el-dialog
    class="associate-dialog"
    title="绑定弹性IP"
    :visible="show"
    width="35vw"
    top="15vh"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    @close="closeClick"
  >
    <el-form
      ref="form"
      class="associate-form"
      :rules="rules"
      :model="form"
      label-width="auto"
      size="medium"
    >
      <el-form-item label="云服务商" prop="cloudProvider">
        <el-input
          v-model="form.cloudProvider"
          placeholder="请选择云服务商"
          :disabled="true"
        />
      </el-form-item>
      <el-form-item label="云账号" prop="cloudAccountName">
        <el-input
          v-model="form.cloudAccountName"
          placeholder="请选择云账号"
          :disabled="true"
        />
      </el-form-item>
      <el-form-item label="资源类型" prop="resType">
        <el-input
          v-model="form.resType"
          placeholder="请选择"
          :disabled="true"
        />
      </el-form-item>
      <el-form-item label="云主机" prop="vmId">
        <el-select
          v-model="form.vmId"
          placeholder="请选择云主机"
          style="width: 100%"
          :loading="loadingVMs"
        >
          <el-option
            v-for="item in vmOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="small" @click="cancelClick">取 消</el-button>
      <el-button
        type="primary"
        size="small"
        :loading="confirmLoading"
        @click="confirmClick"
      >确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {
  associateElasticIP,
  getAssociateElasticIPVMs
} from '@/api/cn-resource'

export default {
  name: 'AssociateDialog',
  components: {},
  mixins: [],
  props: {
    // 是否显示
    show: {
      type: Boolean,
      default: false
    },
    info: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      vmOptions: [], // 云主机列表
      form: {
        id: '',
        cloudProvider: '',
        cloudAccountName: '',
        resType: '',
        vmId: ''
      },
      rules: {
        vmId: [{ required: true, message: '请选择云主机', trigger: 'change' }]
      },
      loadingVMs: false,
      confirmLoading: false
    }
  },
  computed: {},
  watch: {
    info: {
      handler(newVal) {
        if (newVal.id) {
          this.form = {
            ...newVal,
            resType: '云主机'
          }
          this.getVMs()
        }
      },
      immediate: true,
      deep: true
    }
  },
  mounted() {},
  methods: {
    /**
     * @description: 获取云主机列表
     * @return {*}
     */
    getVMs() {
      this.loadingVMs = true
      getAssociateElasticIPVMs({
        cloudAccountName: this.form.cloudAccountName,
        cloudProvider: this.form.cloudProvider,
        cpRegionId: this.form.region
      })
        .then((res) => {
          this.vmOptions = Array.isArray(res.data) ? res.data : []
        })
        .finally(() => {
          this.loadingVMs = false
        })
    },
    /**
     * @description: 确定
     * @return {*}
     */
    confirmClick() {
      this.$refs.form.validate((valid, obj) => {
        if (valid) {
          this.confirmLoading = true
          associateElasticIP({
            id: this.form.id,
            vmId: this.form.vmId
          })
            .then((res) => {
              this.$emit('success')
              this.closeClick()
            })
            .finally(() => {
              this.confirmLoading = false
            })
        }
      })
    },
    /**
     * @description: 关闭
     * @return {*}
     */
    closeClick() {
      this.$emit('update:show', false)
    },
    /**
     * @description: 取消
     * @return {*}
     */
    cancelClick() {
      this.closeClick()
      this.$emit('cancel')
    }
  }
}
</script>

<style lang="less" scoped>
@import "@/assets/css/common.less";
.associate-dialog {
  /deep/ .el-dialog {
    min-width: 600px;
  }
}
</style>
